"""
Device connector factory for creating connector instances
"""

from __future__ import annotations

from enum import Enum
from typing import Any

from app.connectors.base import BaseConnector
from app.connectors.http_connector import HTTPConnector
from app.connectors.modbus_tcp_connector import ModbusTCPConnector
from app.connectors.opcua_connector import OPCUAConnector


class DeviceType(str, Enum):
    """Device type enumeration"""
    HTTP = "http"
    OPC_UA = "opc_ua"
    MODBUS_TCP = "modbus_tcp"
    # Future connector types:
    # MODBUS_RTU = "modbus_rtu"
    # MQTT = "mqtt"
    # SERIAL = "serial"


class DeviceConnectorFactory:
    """
    Device connector factory

    Provides unified interface for creating and managing device connectors
    """

    # Connector registry
    _connectors: dict[DeviceType, type[BaseConnector]] = {
        DeviceType.HTTP: HTTPConnector,
        DeviceType.OPC_UA: OPCUAConnector,
        DeviceType.MODBUS_TCP: ModbusTCPConnector,
        # Future connectors will be registered here
        # DeviceType.MODBUS_RTU: SerialConnector,
        # DeviceType.MQTT: MQTTConnector,
        # DeviceType.SERIAL: SerialConnector,
    }

    @classmethod
    def create(
        cls,
        device_type: DeviceType,
        config: dict[str, Any]
    ) -> BaseConnector:
        """
        Create connector instance

        Args:
            device_type: Device type
            config: Connection configuration

        Returns:
            BaseConnector: Connector instance

        Raises:
            ValueError: Unsupported device type

        Example:
            >>> config = {
            ...     "device_id": 1,
            ...     "base_url": "http://example.com"
            ... }
            >>> connector = DeviceConnectorFactory.create(
            ...     DeviceType.HTTP,
            ...     config
            ... )
        """
        connector_class = cls._connectors.get(device_type)

        if not connector_class:
            raise ValueError(
                f"Unsupported device type: {device_type}. "
                f"Supported types: {list(cls._connectors.keys())}"
            )

        # Special handling for Modbus RTU (future implementation)
        # if device_type == DeviceType.MODBUS_RTU:
        #     config["protocol"] = "modbus_rtu"

        return connector_class(config)

    @classmethod
    def register_connector(
        cls,
        device_type: DeviceType,
        connector_class: type[BaseConnector]
    ) -> None:
        """
        Register custom connector

        Allows users to extend supported device types

        Args:
            device_type: Device type to register
            connector_class: Connector class implementing BaseConnector

        Example:
            >>> class CustomConnector(BaseConnector):
            ...     # Custom implementation
            ...     pass
            >>>
            >>> DeviceConnectorFactory.register_connector(
            ...     DeviceType.CUSTOM,
            ...     CustomConnector
            ... )
        """
        cls._connectors[device_type] = connector_class

    @classmethod
    def get_supported_types(cls) -> list[DeviceType]:
        """
        Get list of supported device types

        Returns:
            list[DeviceType]: List of supported device types
        """
        return list(cls._connectors.keys())
